package com.oceansoft.core.jxkh.dao.impl;

import com.hui.platform.system.exception.HuiException;
import com.hui.platform.system.utility.StringUtil;
import com.oceansoft.core.common.util.PageUtil;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.domain.SysUser;
import com.oceansoft.core.jxkh.dao.ISigninDao;
import com.oceansoft.core.jxkh.dao.IWorkLogDao;
import com.oceansoft.core.jxkh.domain.Daliywork;
import com.oceansoft.core.jxkh.domain.Signin;
import com.oceansoft.core.jxkh.domain.WorkLog;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/10/24.
 */
@Repository
public class SigninDaoImpl implements ISigninDao {


    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public int countSignToday(String policeNum) throws HuiException {
        String sql = "select count(1) from T_JXKH_SIGNIN where policenum= '"+policeNum+"'"+" and trunc(SIGNINTIME) = trunc(sysdate)";
        return jdbcTemplate.queryForInt(sql);
    }

    @Override
    public void signOut(String policeNum)throws HuiException  {
        String sql = "update T_JXKH_SIGNIN set signouttime = sysdate where policenum= '"+policeNum+"'"+" and trunc(SIGNINTIME) = trunc(sysdate)";
        jdbcTemplate.update(sql);
    }

    @Override
    public void signIn(String policeNum)throws HuiException  {
        String sql = "insert into T_JXKH_SIGNIN (dbid,SIGNINTIME,SIGNOUTTIME,POLICENUM) values(SEQ_T_JXKH_SIGNIN.NEXTVAL,SYSDATE,NULL,?)";
        jdbcTemplate.update(sql,policeNum);
    }

    @Override
    public int countSignOutToday(String policeNum) {
        String sql = "select count(1) from T_JXKH_SIGNIN where policenum= '"+policeNum+"'"+" and trunc(SIGNOUTTIME) = trunc(sysdate)";
        return jdbcTemplate.queryForInt(sql);
    }

    @Override
    public Signin findByPoliceNum(String policeNum) {
        String sql = "select t.dbid,to_char(t.SIGNINTIME,'HH24:MI:SS') SIGNINTIME ,to_char(t.SIGNOUTTIME,'HH24:MI:SS') SIGNOUTTIME,T.POLICENUM  from T_JXKH_SIGNIN t where policenum= '"+policeNum+"'"+" and trunc(SIGNINTIME) = trunc(sysdate)";
        List<Signin> list = jdbcTemplate.query(sql, new RowMapper<Signin>() {
            @Override
            public Signin mapRow(ResultSet rs, int arg1) throws SQLException {
                Signin sign = new Signin();
                sign.setDbid(rs.getInt("DBID"));
                sign.setPolicenum(rs.getString("POLICENUM"));
                sign.setSignintime(rs.getString("SIGNINTIME"));
                sign.setSignouttime(rs.getString("SIGNOUTTIME"));
                return sign;
            }
        });
        if(list.size()>0){
            return list.get(0);
        }
        return new Signin();
    }

    @Override
    public String getNowTime() {
        return jdbcTemplate.queryForObject("select TO_CHAR(sysdate, 'hh24:mi:ss') from dual ",String.class);
    }

    @Override
    public Signin findByPoliceNumAndDate(String policeNum, String workdate) {
        String sql = "select t.dbid,to_char(t.SIGNINTIME,'YYYY-MM-DD HH24:MI:SS') SIGNINTIME ,to_char(t.SIGNOUTTIME,'YYYY-MM-DD HH24:MI:SS') SIGNOUTTIME,T.POLICENUM  from T_JXKH_SIGNIN t where policenum= '"+policeNum+"'"+" and trunc(SIGNINTIME) = to_date('"+workdate+"','YYYY-MM-DD')";
        List<Signin> list = jdbcTemplate.query(sql, new RowMapper<Signin>() {
            @Override
            public Signin mapRow(ResultSet rs, int arg1) throws SQLException {
                Signin sign = new Signin();
                sign.setDbid(rs.getInt("DBID"));
                sign.setPolicenum(rs.getString("POLICENUM"));
                sign.setSignintime(rs.getString("SIGNINTIME"));
                sign.setSignouttime(rs.getString("SIGNOUTTIME"));
                return sign;
            }
        });
        if(list.size()>0){
            return list.get(0);
        }
        return new Signin();
    }
}
